/**
 * Copyright 2013-present febit.org (support@febit.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.febit.generator.model;

import java.util.Iterator;
import java.util.TreeSet;
import org.febit.convert.Convert;
import org.febit.util.StringUtil;

/**
 *
 * @author zqq90
 */
public class DependLibs extends TreeSet<DependLib> {

    static {
        Convert.register(DependLibs.class, (String raw, Class type1) -> DependLibs.valueOf(raw));
    }

    public static void noop() {
        // Do nothing
    }

    public static DependLibs valueOf(String raw) {
        DependLibs libs = new DependLibs();
        for (String str : StringUtil.toArrayExcludeCommit(raw)) {
            libs.add(DependLib.valueOf(str));
        }
        libs.clearDupArtifacts();
        return libs;
    }

    public void clearDupArtifacts() {
        final Iterator<DependLib> it = descendingIterator();
        if (it.hasNext()) {
            DependLib preDepend = it.next();
            while (it.hasNext()) {
                DependLib dependLib = it.next();
                if (dependLib.isSameArtifact(preDepend)) {
                    it.remove();
                } else {
                    preDepend = dependLib;
                }
            }
        }
    }

}
